/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package origendatos.dyd.model;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import origendatos.dyd.model.exceptions.IllegalOrphanException;
import origendatos.dyd.model.exceptions.NonexistentEntityException;
import origendatos.dyd.model.exceptions.PreexistingEntityException;

/**
 *
 * @author erodrig
 */
public class CaracteristicasJpaController {

    public CaracteristicasJpaController() {
        emf = Persistence.createEntityManagerFactory("OrigenDatosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Caracteristicas caracteristicas) throws PreexistingEntityException, Exception {
        if (caracteristicas.getCaracteristicasPjList() == null) {
            caracteristicas.setCaracteristicasPjList(new ArrayList<CaracteristicasPj>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CaracteristicasPj> attachedCaracteristicasPjList = new ArrayList<CaracteristicasPj>();
            for (CaracteristicasPj caracteristicasPjListCaracteristicasPjToAttach : caracteristicas.getCaracteristicasPjList()) {
                caracteristicasPjListCaracteristicasPjToAttach = em.getReference(caracteristicasPjListCaracteristicasPjToAttach.getClass(), caracteristicasPjListCaracteristicasPjToAttach.getCaracteristicasPjPK());
                attachedCaracteristicasPjList.add(caracteristicasPjListCaracteristicasPjToAttach);
            }
            caracteristicas.setCaracteristicasPjList(attachedCaracteristicasPjList);
            em.persist(caracteristicas);
            for (CaracteristicasPj caracteristicasPjListCaracteristicasPj : caracteristicas.getCaracteristicasPjList()) {
                Caracteristicas oldCaracteristicasOfCaracteristicasPjListCaracteristicasPj = caracteristicasPjListCaracteristicasPj.getCaracteristicas();
                caracteristicasPjListCaracteristicasPj.setCaracteristicas(caracteristicas);
                caracteristicasPjListCaracteristicasPj = em.merge(caracteristicasPjListCaracteristicasPj);
                if (oldCaracteristicasOfCaracteristicasPjListCaracteristicasPj != null) {
                    oldCaracteristicasOfCaracteristicasPjListCaracteristicasPj.getCaracteristicasPjList().remove(caracteristicasPjListCaracteristicasPj);
                    oldCaracteristicasOfCaracteristicasPjListCaracteristicasPj = em.merge(oldCaracteristicasOfCaracteristicasPjListCaracteristicasPj);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCaracteristicas(caracteristicas.getAbreviatura()) != null) {
                throw new PreexistingEntityException("Caracteristicas " + caracteristicas + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Caracteristicas caracteristicas) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteristicas persistentCaracteristicas = em.find(Caracteristicas.class, caracteristicas.getAbreviatura());
            List<CaracteristicasPj> caracteristicasPjListOld = persistentCaracteristicas.getCaracteristicasPjList();
            List<CaracteristicasPj> caracteristicasPjListNew = caracteristicas.getCaracteristicasPjList();
            List<String> illegalOrphanMessages = null;
            for (CaracteristicasPj caracteristicasPjListOldCaracteristicasPj : caracteristicasPjListOld) {
                if (!caracteristicasPjListNew.contains(caracteristicasPjListOldCaracteristicasPj)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CaracteristicasPj " + caracteristicasPjListOldCaracteristicasPj + " since its caracteristicas field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CaracteristicasPj> attachedCaracteristicasPjListNew = new ArrayList<CaracteristicasPj>();
            for (CaracteristicasPj caracteristicasPjListNewCaracteristicasPjToAttach : caracteristicasPjListNew) {
                caracteristicasPjListNewCaracteristicasPjToAttach = em.getReference(caracteristicasPjListNewCaracteristicasPjToAttach.getClass(), caracteristicasPjListNewCaracteristicasPjToAttach.getCaracteristicasPjPK());
                attachedCaracteristicasPjListNew.add(caracteristicasPjListNewCaracteristicasPjToAttach);
            }
            caracteristicasPjListNew = attachedCaracteristicasPjListNew;
            caracteristicas.setCaracteristicasPjList(caracteristicasPjListNew);
            caracteristicas = em.merge(caracteristicas);
            for (CaracteristicasPj caracteristicasPjListNewCaracteristicasPj : caracteristicasPjListNew) {
                if (!caracteristicasPjListOld.contains(caracteristicasPjListNewCaracteristicasPj)) {
                    Caracteristicas oldCaracteristicasOfCaracteristicasPjListNewCaracteristicasPj = caracteristicasPjListNewCaracteristicasPj.getCaracteristicas();
                    caracteristicasPjListNewCaracteristicasPj.setCaracteristicas(caracteristicas);
                    caracteristicasPjListNewCaracteristicasPj = em.merge(caracteristicasPjListNewCaracteristicasPj);
                    if (oldCaracteristicasOfCaracteristicasPjListNewCaracteristicasPj != null && !oldCaracteristicasOfCaracteristicasPjListNewCaracteristicasPj.equals(caracteristicas)) {
                        oldCaracteristicasOfCaracteristicasPjListNewCaracteristicasPj.getCaracteristicasPjList().remove(caracteristicasPjListNewCaracteristicasPj);
                        oldCaracteristicasOfCaracteristicasPjListNewCaracteristicasPj = em.merge(oldCaracteristicasOfCaracteristicasPjListNewCaracteristicasPj);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = caracteristicas.getAbreviatura();
                if (findCaracteristicas(id) == null) {
                    throw new NonexistentEntityException("The caracteristicas with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteristicas caracteristicas;
            try {
                caracteristicas = em.getReference(Caracteristicas.class, id);
                caracteristicas.getAbreviatura();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The caracteristicas with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CaracteristicasPj> caracteristicasPjListOrphanCheck = caracteristicas.getCaracteristicasPjList();
            for (CaracteristicasPj caracteristicasPjListOrphanCheckCaracteristicasPj : caracteristicasPjListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Caracteristicas (" + caracteristicas + ") cannot be destroyed since the CaracteristicasPj " + caracteristicasPjListOrphanCheckCaracteristicasPj + " in its caracteristicasPjList field has a non-nullable caracteristicas field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(caracteristicas);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Caracteristicas> findCaracteristicasEntities() {
        return findCaracteristicasEntities(true, -1, -1);
    }

    public List<Caracteristicas> findCaracteristicasEntities(int maxResults, int firstResult) {
        return findCaracteristicasEntities(false, maxResults, firstResult);
    }

    private List<Caracteristicas> findCaracteristicasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Caracteristicas.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Caracteristicas findCaracteristicas(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Caracteristicas.class, id);
        } finally {
            em.close();
        }
    }

    public int getCaracteristicasCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Caracteristicas> rt = cq.from(Caracteristicas.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
